package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.entity.Role;
import com.aaa.vegetablemarketexpress.service.RoleService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @FileName: RoleController
 * @Description: 角色管理控制器
 * @Author: 张钰哲
 * @CreateTime: 2025/9/9 10:57
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Resource
    private RoleService roleService;

    /**
     * 分页查询
     * @param page
     * @return
     */
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<Role> page) {
        PageInfo<Role> rolePageInfo = roleService.queryByPage(page);
        return ResultUtil.success(rolePageInfo);
    }

    /**
     * 根据ID查询角色详情
     * @param roleId
     * @return
     */
    @GetMapping("queryByRoleId")
    public Result<Role> queryByRoleId(Long roleId) {
        Role role = roleService.queryByRoleId(roleId);
        return ResultUtil.success(role);
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Role role) {
        int result = roleService.add(role);
        return ResultUtil.success(result);
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody Role role) {

        int result = roleService.update(role);
        return ResultUtil.success(result);

    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("delete")
    public Result delete(Long roleId) {
        int result = roleService.delete(roleId);
        return ResultUtil.success(result);
    }

    /**
     * 检查角色名称是否唯一
     * @param roleName
     * @return
     */
    @GetMapping("checkRoleNameUnique")
    public Result checkRoleNameUnique(String roleName) {
        Role role = roleService.checkRoleNameUnique(roleName);
        return ResultUtil.success(role);
    }
}
